<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Service\User;

use App\Models\VoucherPackage;
use App\Models\VoucherPackageTransaction;
use App\Service\BaseService;

class VoucherPackageService extends BaseService
{
    public const TYPE_EXCHANGE = 1; //消费券

    protected $type;

    public function __construct(int $type = self::TYPE_EXCHANGE)
    {
        $this->type = $type;
    }

    public function isItSufficient($user_id, $amount): bool
    {
        $account = VoucherPackage::query()
            ->where('type', $this->type)
            ->where('user_id', $user_id)
            ->first();
        $current_quantity = $account->current_quantity ?? 0;
        if ($current_quantity < $amount) {
            return false;
        }
        return true;
    }

    /**
     * 充值
     * @param mixed $user_id
     * @param mixed $amount
     * @param mixed $describe
     * @return Builder|Builder[]|Collection|false|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|Model|void
     */
    public function recharge($user_id, $amount, $describe = '获取')
    {
        if ($amount < 0) {
            return false;
        }
        if ($describe == '获取') {
            switch ($this->type) {
                case self::TYPE_EXCHANGE:
                    $msg = sprintf('获取兑换券:%s', $amount);
                    break;
            }
        }
        $account = VoucherPackage::query()
            ->where('type', $this->type)
            ->where('user_id', $user_id)->firstOrCreate([
                'user_id' => $user_id,
            ], [
                'type' => $this->type,
            ]);
        $account->total_number += $amount;
        $account->current_quantity += $amount;
        $account->save();
        $this->transactionHistory($user_id, $amount, $account->current_quantity, $msg, 1);
        return $account;
    }

    /**
     * @param mixed $user_id
     * @param mixed $amount
     * @param mixed $type
     * @param mixed $describe
     * @return Builder|Builder[]|Collection|false|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|Model|void
     */
    public function consumption($user_id, $amount, $describe = '消费')
    {
        $account = VoucherPackage::query()
            ->where('type', $this->type)
            ->where('user_id', $user_id)->firstOrCreate([
                'user_id' => $user_id,
            ]);
        if ($account->current_quantity < $amount) {
            return false;
        }
        $msg = sprintf('消费兑换券:%s', $amount);
        if ($describe == '消费') {
            switch ($this->type) {
                case self::TYPE_EXCHANGE:
                    $msg = sprintf('消费兑换券:%s', $amount);
                    break;
            }
        }
        $account->current_quantity -= $amount;
        $account->usage_quantity += $amount;
        $account->save();
        $this->transactionHistory($user_id, $amount, $account->current_quantity, $msg, 2);
        return $account;
    }

    /**
     * 交易记录.
     * @param mixed $user_id
     * @param mixed $amount
     * @param mixed $balance
     * @param mixed $describe
     * @param mixed $change_type
     */
    private function transactionHistory($user_id, $amount, $balance, $describe, $change_type): void
    {
        VoucherPackageTransaction::query()->create([
            'user_id' => $user_id,
            'amount' => $amount,
            'balance' => $balance,
            'describe' => $describe,
            'change_type' => $change_type,
            'type' => $this->type,
        ]);
    }
}
